package com.yjq.programmer.service.impl;

import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.CollectMapper;
import com.yjq.programmer.dao.MusicMapper;
import com.yjq.programmer.domain.Collect;
import com.yjq.programmer.domain.CollectExample;
import com.yjq.programmer.domain.Music;
import com.yjq.programmer.domain.MusicExample;
import com.yjq.programmer.dto.CollectDTO;
import com.yjq.programmer.dto.MusicDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.dto.UserDTO;
import com.yjq.programmer.service.ICollectService;
import com.yjq.programmer.service.IUserService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-27 20:39
 */
@Service
@Transactional
public class CollectServiceImpl implements ICollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private IUserService userService;

    @Resource
    private MusicMapper musicMapper;

    /**
     * 添加音乐到收藏操作
     * @param collectDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> addMusicToCollect(CollectDTO collectDTO) {
        if(CommonUtil.isEmpty(collectDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 登录信息验证
        UserDTO userDTO = new UserDTO(collectDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        collectDTO.setUserId(userDTO.getId());
        // 获取对应音乐信息
        Music music = musicMapper.selectByPrimaryKey(collectDTO.getMusicId());
        if(music == null) {
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_NOT_EXIST);
        }
        Collect collect = CopyUtil.copy(collectDTO, Collect.class);
        collect.setId(UuidUtil.getShortUuid());
        collect.setCreateTime(new Date());
        if(collectMapper.insertSelective(collect) == 0) {
            return ResponseDTO.errorByMsg(CodeMsg.COLLECT_ADD_ERROR);
        }
        // 对应音乐的收藏数增加
        music.setCollectNum(music.getCollectNum() + 1);
        musicMapper.updateByPrimaryKeySelective(music);
        return ResponseDTO.successByMsg(true, "收藏成功！");
    }

    /**
     * 取消音乐收藏操作
     * @param collectDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removeMusicFromCollect(CollectDTO collectDTO) {
        if(CommonUtil.isEmpty(collectDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 登录信息验证
        UserDTO userDTO = new UserDTO(collectDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        // 获取对应音乐信息
        Music music = musicMapper.selectByPrimaryKey(collectDTO.getMusicId());
        if(music == null) {
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_NOT_EXIST);
        }
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userDTO.getId()).andMusicIdEqualTo(collectDTO.getMusicId());
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        if(collectList == null || collectList.size() != 1) {
            return ResponseDTO.errorByMsg(CodeMsg.COLLECT_NOT_EXIST);
        }
        Collect collect = collectList.get(0);
        if(collectMapper.deleteByPrimaryKey(collect.getId()) == 0) {
            return ResponseDTO.errorByMsg(CodeMsg.COLLECT_REMOVE_ERROR);
        }
        // 对应音乐的收藏数减少
        music.setCollectNum(music.getCollectNum() - 1);
        musicMapper.updateByPrimaryKeySelective(music);
        return ResponseDTO.successByMsg(true, "取消收藏成功！");
    }

    /**
     * 判断用户是否收藏某个音乐
     * @param collectDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> isCollectMusic(CollectDTO collectDTO) {
        if(CommonUtil.isEmpty(collectDTO.getMusicId())) {
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 登录信息验证
        UserDTO userDTO = new UserDTO(collectDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        // 获取对应音乐信息
        Music music = musicMapper.selectByPrimaryKey(collectDTO.getMusicId());
        if(music == null) {
            return ResponseDTO.errorByMsg(CodeMsg.MUSIC_NOT_EXIST);
        }
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userDTO.getId()).andMusicIdEqualTo(collectDTO.getMusicId());
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        if(collectList != null && collectList.size() > 0) {
            return ResponseDTO.success(true);
        } else {
            return ResponseDTO.success(false);
        }
    }

    /**
     * 根据用户id获取收藏数据
     * @param collectDTO
     * @return
     */
    @Override
    public ResponseDTO<List<MusicDTO>> getCollectByUserId(CollectDTO collectDTO) {
        // 登录信息验证
        UserDTO userDTO = new UserDTO(collectDTO.getToken());
        ResponseDTO<UserDTO> loginUserResponse = userService.getLoginUser(userDTO);
        if(!CodeMsg.SUCCESS.getCode().equals(loginUserResponse.getCode())) {
            return ResponseDTO.errorByMsg(CodeMsg.USER_SESSION_EXPIRED);
        }
        userDTO = loginUserResponse.getData();
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(userDTO.getId());
        collectExample.setOrderByClause("create_time desc");
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        List<String> musicIdList = collectList.stream().map(Collect::getMusicId).collect(Collectors.toList());
        MusicExample musicExample = new MusicExample();
        if(musicIdList.size() > 0) {
            musicExample.createCriteria().andIdIn(musicIdList);
        } else {
            return ResponseDTO.success(new ArrayList<>());
        }
        List<Music> musicList = musicMapper.selectByExample(musicExample);
        return ResponseDTO.success(CopyUtil.copyList(musicList, MusicDTO.class));
    }
}
